﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using MotionIoLib;
namespace OtherDevice
{
    public partial class Form_LylinerControl : Form_ControlBase
    {
        public Form_LylinerControl()
        {
            InitializeComponent();
        }
        public void ChangedIoInState(string name, bool bStateCurrent)
        {

            if (InvokeRequired)
            {
                this.BeginInvoke(new Action(() => ChangedIoInState(name, bStateCurrent)));
            }
            else
            {
                if (name == clyLiner.paramter.str_前段感应到位信号)
                    IO_Forward.State = bStateCurrent;
                if (name == clyLiner.paramter.str_后段感应到位信号)
                    IO_Back.State = bStateCurrent;
            }
        }
        public void ChangedRobotIoInState(string strRobotName, string name, bool bStateCurrent)
        {

            if (InvokeRequired)
            {
                this.BeginInvoke(new Action(() => ChangedRobotIoInState(strRobotName,name, bStateCurrent)));
            }
            else
            {
                if(strRobotName== clyLiner.paramter.str_机器人名称)
                {
                    if (name == clyLiner.paramter.str_前段感应到位信号)
                        IO_Forward.State = bStateCurrent;
                    if (name == clyLiner.paramter.str_后段感应到位信号)
                        IO_Back.State = bStateCurrent;
                }
            }
        }
        ClylinerBase clyLiner = null;
        public override void FlushToDlg(object devicebase)
        {
            try
            {
                if (devicebase == null)
                    return;
                this.clyLiner = devicebase as ClylinerBase;
                this.Dock = DockStyle.Fill;
                this.Show();
            }
            catch (Exception)
            {

            }

        }

        private void IO_Out_Click(object sender, EventArgs e)
        {

        }

        private void Form_LylinerControl_Load(object sender, EventArgs e)
        {
            if (clyLiner.paramter.b_是否为机器人气缸)
            {
                RobotMotionMgr.GetInstace().m_eventIoInputChanage += ChangedRobotIoInState;
                if (RobotMotionMgr.GetInstace().dic_Input.ContainsKey(clyLiner.paramter.str_前段感应到位信号))
                {
                    IO_Forward.State = RobotMotionMgr.GetInstace().GetInBit(clyLiner.paramter.str_机器人名称, clyLiner.paramter.str_前段感应到位信号);
                }
                if (RobotMotionMgr.GetInstace().dic_Output.ContainsKey(clyLiner.paramter.str_后段感应到位信号))
                {
                    IO_Back.State = RobotMotionMgr.GetInstace().GetInBit(clyLiner.paramter.str_机器人名称, clyLiner.paramter.str_后段感应到位信号);
                }

            }
            else
            {
                IOMgr.GetInstace().m_eventIoInputChanageByName += ChangedIoInState;
                if (IOMgr.GetInstace().GetInputDic().ContainsKey(clyLiner.paramter.str_前段感应到位信号))
                {
                    IO_Forward.State = IOMgr.GetInstace().ReadIoInBit(clyLiner.paramter.str_前段感应到位信号);
                }
                if (IOMgr.GetInstace().GetInputDic().ContainsKey(clyLiner.paramter.str_后段感应到位信号))
                {
                    IO_Back.State = IOMgr.GetInstace().ReadIoInBit(clyLiner.paramter.str_后段感应到位信号);
                }
            }
            IO_Forward.IOName = clyLiner.paramter.str_前段感应到位信号;
            IO_Forward.Height = 40;
            IO_Back.IOName = clyLiner.paramter.str_后段感应到位信号;
            IO_Back.Height = 40;

        }




        private void btn_Ahead_MouseDown(object sender, MouseEventArgs e)
        {
            clyLiner.Function_前进(cbb_Inpos.Checked);
        }

        private void btn_Ahead_MouseUp(object sender, MouseEventArgs e)
        {
            if(clyLiner.paramter.b_是否为双控电磁阀)
               clyLiner.Function_中断停止();
        }

        private void btn_Back_MouseDown(object sender, MouseEventArgs e)
        {
            clyLiner.Function_后退(cbb_Inpos.Checked);
        }

        private void btn_Back_MouseUp(object sender, MouseEventArgs e)
        {
            if (clyLiner.paramter.b_是否为双控电磁阀)
                clyLiner.Function_中断停止();
        }
    }
}
